/*
 * Package il.ac.biu.cs.grossmm.api.data
 * File Pair.java
 * 
 * This is a part of presence service framework API. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.api.data;

/**
 * Represents a tuple of two objects
 */
public class Pair<F, S> {

	private final F first;
	private final S second;
	
	protected Pair(F first, S second)
	{
		this.first = first;
		this.second = second;
	}
	
	/**
	 * Creates a new tuple
	 * @param <F> first object type
	 * @param <S> second object type
	 * @param first the first object
	 * @param second the second object
	 * @return the tuple
	 */
	public static <F, S> Pair<F, S> pair(F first, S second) {
		return new Pair<F, S>(first, second);
	}

	/**
	 * Gets the first object in the tuple
	 * @return the first object in the tuple
	 */
	public final F first()
	{
		return first;
	}
	
	/**
	 * Gets the secondobject in the tuple
	 * @return the first object in the tuple
	 */
	public final S second()
	{
		return second;
	}

	@Override
	public int hashCode()
	{
		int x = first != null ?  first.hashCode()*257 : 0;
		int y = second != null ? second.hashCode(): 0;
			
			
		return x * y;
	}
	
	@Override
	public boolean equals(Object obj)
	{
		if (obj == null)
			return false;

		if (!(obj instanceof Pair))
			return false;

		Pair pair = (Pair) obj;

		return first == null ? pair.first == null : first.equals(pair.first)
			&& second == null ? pair.second == null
			: second.equals(pair.second);
	}
	
	@Override
	public String toString()
	{
		String f = first.toString();
		String s = second.toString();
		
		StringBuffer sb = new StringBuffer(f.length() + s.length() + 4);
		
		sb.append("(").append(f).append(", ").append(s).append(")");
		
		return sb.toString();
	}
}
